<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
    <meta http-equiv="content-language" content="zh-CN" />
    

    
    <meta name="viewport" content="width=device-width, initial-scale=0.5">
    

    
    <title>matlab优化工具02非线性规划之fmincon</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
    
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css">

    <link rel="stylesheet" href="/css/stylesheet.css">
    <link rel="stylesheet" href="/css/home.css">

    
    
        <style type="text/css">
        body { background-color: #fbf6ec;}
        </style>
    
    
                
        
        
            <link rel="stylesheet" href="/css/main.css"/>
        




        
        
        
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/styles/github.min.css"  />
         
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/highlight.min.js"></script>
        
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/r.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/yaml.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/latex.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/matlab.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/mathematica.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia-repl.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/powershell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/bash.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/shell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/python.min.js"></script>
        
        <script>hljs.initHighlightingOnLoad();</script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
     
     
</head>


<body>
    <script>
        window.addEventListener("resize", resizeThrottler, false);

        var resizeTimeout;
        function resizeThrottler() {
        
        if ( !resizeTimeout ) {
            resizeTimeout = setTimeout(function() {
            resizeTimeout = null;
            actualResizeHandler();
        
            
            }, 66);
        }
        }
        actualResizeHandler()
        function actualResizeHandler() {
                if (/mobile/i.test(navigator.userAgent) || /android/i.test(navigator.userAgent))
                {
                    document.body.classList.add('mobile');
                }else{
                    document.body.classList.remove('mobile');  
                }
    }</script>

    
      
      
            <nav class="navbar navbar-default navbar-static-top" style="opacity: .9" role="navigation">
        <div class="container-fluid">
            
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">

                    <span class="sr-only">Toggle navigation</span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>

                </button>
                <a class="navbar-brand" href="/">zsc</a>
            </div>

            <div class="navbar-collapse collapse" id="bs-example-navbar-collapse-1" style="height: auto;">
                <ul class="nav navbar-nav navbar-right" style="font-size: 100%">
                    
                        
                            
                            <li class=""><a href="/about/">About</a></li>
                            
                            <li class=""><a href="/categories/">Categories</a></li>
                            
                            <li class=""><a href="/">Home</a></li>
                            
                            <li class=""><a href="/tags/">Tags</a></li>
                            
                            <li class=""><a href="/issue/">存在的问题</a></li>
                            
                        
                    
                </ul>
            </div>
        </div>
    </nav>







<div class = "div-content" id='div-content-my' style='display: none;' >
    

    <div class = 'inner-left' id= 'divTableOfContents' style="position:fixed;z-index:999;height: 55%;overflow: scroll;bottom: 5%;width: 22%;top: 25%" >
            <p class="slide slidemy" align = "center">
                <a href="javascript:hidediv();" id="strHref" class="btn-slide">目录收起-</a>
            </p>
            
            <div id="divtocTableOfContents">
            <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li></li>
        <li><a href="#例">例:</a></li>
        <li><a href="#模型-常用非线性规划模型我">模型: 常用非线性规划模型(我)</a></li>
      </ul>
    </li>
  </ul>
</nav>
            </div>
    </div>
</div>
<script>  
    $(document).ready(function () {
    var demo = $("#divtocTableOfContents").find("a").length;
    if(demo > 2){
        $("div#div-content-my").fadeIn("slow");
    }
        
        
        
        
        
        
        
        
    }); 
</script>  









<div class="inner">
    



    <div class="blog-post">
        
                <div>
            <h2 align="center" id = "singe-h2">
                matlab优化工具02非线性规划之fmincon
                <time>
                    <br>
                    <span> 
                        <i class="fa fa-user-edit" style="color:#888;font-size: 80%;"></i>
                        zsc 
                    </span>
                    &nbsp 
                    <span>                 
                        <i class="fa fa-calendar-alt" style="color:#888;font-size: 80%;"></i>
                        2021-05-16 
                    </span>
                </time>
                
                
                <div>
                    <ul class="tags">
                        
                        <span>标签:</span>
                        <li><a class="link" href="/tags/matlab"> #matlab </a></li>
                        
                        <span> </span>
                        
                    </ul>
                    
                </div>
            </h2>
        </div>
    
        
        <section id="content">
            <h1 id="matlab优化工具02非线性规划之fmincon">matlab优化工具02非线性规划之fmincon</h1>
<p>由于经常用到一些matlab中基本的优化函数, 于是写一个笔记, 由于新版本的文档和以前版本的文档有点不一样, 搞得查起来有点费劲, 不过推荐新版本的文档</p>
<h4 id="非线性规划的标准型及参数解释">非线性规划的标准型及参数解释</h4>
<p>$$
\begin{aligned}
&amp; \min \quad  f(x) \
&amp; \text {s.t.} \begin{cases}
\textbf{A} \cdot x \leq b \
\textbf{Aeq} \cdot x=beq \
c(x) \leq 0 \
\operatorname{ceq}(x)=0 \
l b \leq x \leq u b
\end{cases}
\end{aligned}
$$</p>
<p>其中 $f(x)$是目标函数, $x, b, beq$ 是向量, $\textbf{A}, \textbf{Aeq}$是矩阵,$c(x)$ 和 $ceq(x)$ 是向量函數, $\textbf{A}$线性不等式,$\textbf{Aeq}$线性等式, $c(x)$ 非线性不等式, $ceq(x)$非线性等式</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-MATLAB" data-lang="MATLAB"><span style="display:flex;"><span><span style="color:#75715e">% 2.基本语法</span>
</span></span><span style="display:flex;"><span>[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
</span></span><span style="display:flex;"><span>[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(___)
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 等号左边参数解释</span>
</span></span><span style="display:flex;"><span>x 的返回值是决策向量x的取值，fval 的返回值是目标函数f(x)的取值
</span></span><span style="display:flex;"><span>exitflag 参数，描述函数计算的退出条件
</span></span><span style="display:flex;"><span>output, 输出模型的优化信息参数
</span></span><span style="display:flex;"><span>lambda, 返回解x处包含拉格朗日乘子的lambda参数
</span></span><span style="display:flex;"><span>grad, 返回解x处fun函数的梯度值
</span></span><span style="display:flex;"><span>hessian, 返回解x处fun函数的hessian矩阵
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 等号右边参数解释</span>
</span></span><span style="display:flex;"><span>fun是用M文件定义的函数f(x),代表了(非)线性目标函数, 对于复杂问题, 建议写出目标函数以及非线性约束的梯度
</span></span><span style="display:flex;"><span>x0是x的初始值
</span></span><span style="display:flex;"><span>A,b,Aeq,beq定义了线性约束 ,如果没有线性约束，则A=[],b=[],Aeq=[],beq=[]
</span></span><span style="display:flex;"><span>lb和ub是变量x的下界和上界，如果下界和上界没有约束，则lb=[],ub=[],也可以写成lb的各分量都为 <span style="color:#f92672">-</span>inf, ub的各分量都为inf
</span></span><span style="display:flex;"><span>nonlcon是用M文件定义的非线性向量函数约束,如果没有则写[]
</span></span><span style="display:flex;"><span>options定义了优化参数，不填写表示使用Matlab默认的参数设置
</span></span><span style="display:flex;"><span>eg:
</span></span><span style="display:flex;"><span>options = optimoptions(<span style="color:#e6db74">&#39;fmincon&#39;</span>,<span style="color:#e6db74">&#39;Display&#39;</span>,<span style="color:#e6db74">&#39;iter&#39;</span>,<span style="color:#e6db74">&#39;Algorithm&#39;</span>,<span style="color:#e6db74">&#39;sqp&#39;</span>);
</span></span><span style="display:flex;"><span>options = optimoptions(<span style="color:#e6db74">&#39;fmincon&#39;</span>,<span style="color:#e6db74">&#39;SpecifyObjectiveGradient&#39;</span>,true);
</span></span><span style="display:flex;"><span>options = optimoptions(<span style="color:#e6db74">&#39;fmincon&#39;</span>,<span style="color:#e6db74">&#39;Display&#39;</span>,<span style="color:#e6db74">&#39;iter&#39;</span>,<span style="color:#e6db74">&#39;PlotFcn&#39;</span>,<span style="color:#e6db74">&#39;optimplotfval&#39;</span>);
</span></span></code></pre></div><table>
<thead>
<tr>
<th>options 常见取值</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>Algorithm</td>
<td>优化算法:<br> <code>'interior-point'</code> (default)<br><code>'trust-region-reflective'</code><br><code> 'sqp'</code><br><code>'sqp-legacy' (optimoptions only)'</code><br><code>active-set'</code></td>
</tr>
<tr>
<td>Display</td>
<td>如果设置为 off 即不显示输出;设置为 iter 即显示每一次的迭代信息;设置为 final 只显示最终结果</td>
</tr>
<tr>
<td>FinDiffType</td>
<td>变量有限差分梯度的类型。取 &lsquo;forward&rsquo;日才即为向前差分，其为默认值;取 &lsquo;central&rsquo; 时为中心差分，其精度更精确</td>
</tr>
<tr>
<td>FunValCheck</td>
<td>检查目标函数与约束是否都有效。当设置为 on 时，遇到复数、 NaN、Inf 等，即显示 出错信息;当设置为 off时，不显示出错信息，其为默认值</td>
</tr>
<tr>
<td>GradConstr</td>
<td>用户定义的非线性约束函数。当设置为 on 时，返回 4 个输出;设置为 off 时.即为非线性约束的梯度估计有限差</td>
</tr>
<tr>
<td>GradObj</td>
<td>用户定义的目标函数梯度。对于大规模问题为必选项，对中小规模问题为可选项</td>
</tr>
<tr>
<td>MaxFunEvals</td>
<td>函数评价所允许的最大次数</td>
</tr>
<tr>
<td>Maxlter</td>
<td>函数所允许的最大迭代次数</td>
</tr>
<tr>
<td>OutputFcn</td>
<td>在每次迭代中指定一个或多个用户定义的口标优化函数,The default is none ( [ ] ).</td>
</tr>
<tr>
<td>PlotFcn</td>
<td>算法执行时,绘制各种度量值,默认[];<br>&lsquo;optimplotx&rsquo;   画当前点<br>&lsquo;optimplotfunccount&rsquo;  画函数计数<br/>&lsquo;optimplotfval&rsquo;   绘制函数值<br/>&lsquo;optimplotfvalconstr&rsquo;  以直线的形式绘制最佳可行目标函数值。该图显示不可行点为红色，可行点为蓝色，可行性公差为1e-6。<br/>&lsquo;optimplotconstruplication&rsquo;  绘制最大约束冲突<br/>&lsquo;optimplotstepsize&rsquo;  画步长<br/>&lsquo;optimplotfirstorderopt&rsquo;   绘制一阶最优性度量</td>
</tr>
<tr>
<td>FunctionTolerance</td>
<td>函数值的容忍度.默认值为 le-6</td>
</tr>
<tr>
<td>ConstraintTolerance</td>
<td>目标函数的约束性.默认值为 le-6</td>
</tr>
<tr>
<td>UseParallel</td>
<td>是否并行计算</td>
</tr>
</tbody>
</table>
<p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/uPic/202105151217image-20210515121746768.png" alt="image-20210515121746768"></p>
<p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/uPic/202105151218image-20210515121800630.png" alt="image-20210515121800630"></p>
<p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/uPic/202105161223image-20210515121820017.png" alt="image-20210515121820017"></p>
<h4 id="例1-具体例子">例1: 具体例子</h4>
<p>$$
\begin{aligned}
&amp; \min f(x) = x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+8 \
&amp; \text { s. t. }\begin{cases}
x_{1}^{2}-x_{2}+x_{3}^{2} \geq 0 \
x_{1}+x_{2}^{2}+x_{3}^{2} \leq 20 \
-x_{1}-x_{2}^{2}+2=0 \
x_{2}+2 x_{3}^{2}=3 \
x_{1}, x_{2}, x_{3} \geq 0
\end{cases}
\end{aligned}
$$</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>clc, clear all
</span></span><span style="display:flex;"><span>x0 = rand(<span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">1</span>);  <span style="color:#75715e">% 初始值</span>
</span></span><span style="display:flex;"><span>Aeq = [];               <span style="color:#75715e">% 线性等式约束的系数（左边的系数）</span>
</span></span><span style="display:flex;"><span>beq = [];                <span style="color:#75715e">% 线性等式约束的值 （右边的值）</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%A = [];                % 线性不等式约束的系数</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%b = [];                % 线性等式约束的值,(列向量)</span>
</span></span><span style="display:flex;"><span>ub = [];      <span style="color:#75715e">% 变量的上限（取等号）</span>
</span></span><span style="display:flex;"><span>lb = repelem(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">3</span>);      <span style="color:#75715e">% 变量的下限（取等号）</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 方法一: 用句柄函数调用</span>
</span></span><span style="display:flex;"><span>[x, y] = fmincon(@myobjfun, x0, [], [], Aeq, beq, lb, ub, @constrain)
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 方法二: 用如下形式调用</span>
</span></span><span style="display:flex;"><span>[x1, y1] = fmincon(<span style="color:#e6db74">&#39;myobjfun&#39;</span>, x0, [], [], Aeq, beq, lb, ub, <span style="color:#e6db74">&#39;constrain&#39;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 目标函数</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> f = <span style="color:#a6e22e">myobjfun</span>(x)
</span></span><span style="display:flex;"><span>f=x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">8</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 非线性约束条件</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [c,ceq]=<span style="color:#a6e22e">constrain</span>(x)
</span></span><span style="display:flex;"><span>c=[<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>    x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">3</span><span style="color:#f92672">-</span><span style="color:#ae81ff">20</span>]; <span style="color:#75715e">% 非线性不等式约束</span>
</span></span><span style="display:flex;"><span>ceq=[<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>    x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">+</span><span style="color:#ae81ff">2</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">-</span><span style="color:#ae81ff">3</span>]; <span style="color:#75715e">% 非线性等式约束</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 对于非线性约束条件,还可以这样写</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [c,ceq]=<span style="color:#a6e22e">constrain</span>(x)
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 非线性不等式约束</span>
</span></span><span style="display:flex;"><span>c(<span style="color:#ae81ff">1</span>)= <span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>c(<span style="color:#ae81ff">2</span>)=x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">+</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">3</span><span style="color:#f92672">-</span><span style="color:#ae81ff">20</span>; 
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 非线性等式约束</span>
</span></span><span style="display:flex;"><span>ceq(<span style="color:#ae81ff">1</span>) = <span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">+</span><span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>ceq(<span style="color:#ae81ff">2</span>) = x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">+</span><span style="color:#ae81ff">2</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">3</span>)<span style="color:#f92672">.^</span><span style="color:#ae81ff">2</span><span style="color:#f92672">-</span><span style="color:#ae81ff">3</span>; 
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><h4 id="例2-目标函数梯度">例2: 目标函数梯度</h4>
<p>matlab 官网上的例子, 当然如果不设置梯度,也能计算,不过复杂的问题,建议设置梯度</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>options = optimoptions(<span style="color:#e6db74">&#39;fmincon&#39;</span>,<span style="color:#e6db74">&#39;SpecifyObjectiveGradient&#39;</span>,true);
</span></span><span style="display:flex;"><span>fun = @rosenbrockwithgrad;
</span></span><span style="display:flex;"><span>x0 = [<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>];
</span></span><span style="display:flex;"><span>A = [];
</span></span><span style="display:flex;"><span>b = [];
</span></span><span style="display:flex;"><span>Aeq = [];
</span></span><span style="display:flex;"><span>beq = [];
</span></span><span style="display:flex;"><span>lb = [<span style="color:#f92672">-</span><span style="color:#ae81ff">2</span>,<span style="color:#f92672">-</span><span style="color:#ae81ff">2</span>];
</span></span><span style="display:flex;"><span>ub = [<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">2</span>];
</span></span><span style="display:flex;"><span>nonlcon = [];
</span></span><span style="display:flex;"><span>x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 目标函数</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [f,g] = <span style="color:#a6e22e">rosenbrockwithgrad</span>(x)
</span></span><span style="display:flex;"><span><span style="color:#75715e">% Calculate objective f</span>
</span></span><span style="display:flex;"><span>f = <span style="color:#ae81ff">100</span><span style="color:#f92672">*</span>(x(<span style="color:#ae81ff">2</span>) <span style="color:#f92672">-</span> x(<span style="color:#ae81ff">1</span>)^<span style="color:#ae81ff">2</span>)^<span style="color:#ae81ff">2</span> <span style="color:#f92672">+</span> (<span style="color:#ae81ff">1</span><span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>))^<span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> nargout <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">1</span> <span style="color:#75715e">% gradient required</span>
</span></span><span style="display:flex;"><span>    g = [<span style="color:#f92672">-</span><span style="color:#ae81ff">400</span><span style="color:#f92672">*</span>(x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)^<span style="color:#ae81ff">2</span>)<span style="color:#f92672">*</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">-</span><span style="color:#ae81ff">2</span><span style="color:#f92672">*</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>));
</span></span><span style="display:flex;"><span>        <span style="color:#ae81ff">200</span><span style="color:#f92672">*</span>(x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">-</span>x(<span style="color:#ae81ff">1</span>)^<span style="color:#ae81ff">2</span>)];
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><h4 id="例3-约束含有梯度的情形">例3: 约束含有梯度的情形</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#75715e">%%必须是一下情形</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [c, ceq, Gc, Gceq] = <span style="color:#a6e22e">mycon</span>(x)
</span></span><span style="display:flex;"><span>c = <span style="color:#75715e">...          %非线性不等式约束</span>
</span></span><span style="display:flex;"><span>ceq = <span style="color:#75715e">....       % 非线性等式约束</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> nargout <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>	Gc = <span style="color:#75715e">....    % 不等式约束的梯度</span>
</span></span><span style="display:flex;"><span>	Gceq = <span style="color:#75715e">....  % 等式约束的梯度</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p><img src="https://cdn.jsdelivr.net/gh/zscmmm/imgs2208save@master/uPic/202105151151image-20210515115110873.png" alt="image-20210515115110873"></p>
<h3 id="例">例:</h3>
<p>$$
\begin{aligned}
\min &amp; \quad  x_{5} \
&amp; \begin{cases}
x_{3}+9.625 x_{1} x_{4}+16 x_{2} x_{4}+16 x_{4}^{2}+12-4 x_{1}-x_{2}-78 x_{4}=0 \
16 x_{1} x_{4}+44-19 x_{1}-8 x_{2}-x_{3}-24 x_{4}=0 \
-0.25 x_{5}-x_{1} \leq -2.25 \
x_{1}-0.25 x_{5} \leq 2.25 \
-0.5 x_{5}-x_{2} \leq-1.5 \
x_{2}-0.5 x_{5} \leq 1.5 \
-1.5 x_{5}-x_{3} \leq -1.5 \
x_{3}-1.5 x_{5} \leq 1.5
\end{cases}
\end{aligned}
$$</p>
<p>上述问题的局部最优解: <code>[ 1.9638    0.9276   -0.2172    0.0695    1.1448]</code> ,目标函数值为 <code>1.1448</code></p>
<p>全局最优解: <code>[2.4544    1.9088    2.7263    1.3510    0.8175]</code>, 目标函数值为 <code>0.8175</code>. 前4个变量不讨论</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>clc,clear all;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>A = [<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.25</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.25</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.5</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.5</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>];
</span></span><span style="display:flex;"><span>b = [<span style="color:#f92672">-</span><span style="color:#ae81ff">2.25</span>; <span style="color:#ae81ff">2.25</span>; <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>; <span style="color:#ae81ff">1.5</span>; <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>; <span style="color:#ae81ff">1.5</span>];
</span></span><span style="display:flex;"><span>P.objective = @(x)x(<span style="color:#ae81ff">5</span>);
</span></span><span style="display:flex;"><span>P.Aineq = A;
</span></span><span style="display:flex;"><span>P.Bineq = b;
</span></span><span style="display:flex;"><span><span style="color:#75715e">%P.lb = [];</span>
</span></span><span style="display:flex;"><span>P.nonlcon = @mynocon;
</span></span><span style="display:flex;"><span>P.solver = <span style="color:#e6db74">&#39;fmincon&#39;</span>;
</span></span><span style="display:flex;"><span>P.options = optimset;
</span></span><span style="display:flex;"><span>P.x0 = rand(<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>[x,f0,flag] = fmincon(P) <span style="color:#75715e">%给出初值并求解</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 非线性约束</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [c, ceq] = <span style="color:#a6e22e">mynocon</span>(x)
</span></span><span style="display:flex;"><span>c = [];
</span></span><span style="display:flex;"><span>ceq = [x(<span style="color:#ae81ff">3</span>) <span style="color:#f92672">+</span> <span style="color:#ae81ff">9.625</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">*</span>x(<span style="color:#ae81ff">4</span>) <span style="color:#f92672">+</span> <span style="color:#ae81ff">16</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">2</span>)<span style="color:#f92672">*</span>x(<span style="color:#ae81ff">4</span>) <span style="color:#f92672">+</span> <span style="color:#ae81ff">16</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">4</span>)^<span style="color:#ae81ff">2</span> <span style="color:#f92672">+</span> <span style="color:#ae81ff">12</span> <span style="color:#f92672">-</span> <span style="color:#ae81ff">4</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">1</span>) <span style="color:#f92672">-</span> x(<span style="color:#ae81ff">2</span>) <span style="color:#f92672">-</span> <span style="color:#ae81ff">78</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">4</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">16</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">1</span>)<span style="color:#f92672">*</span>x(<span style="color:#ae81ff">4</span>) <span style="color:#f92672">+</span> <span style="color:#ae81ff">44</span> <span style="color:#f92672">-</span> <span style="color:#ae81ff">19</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">1</span>) <span style="color:#f92672">-</span> <span style="color:#ae81ff">8</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">2</span>) <span style="color:#f92672">-</span> x(<span style="color:#ae81ff">3</span>) <span style="color:#f92672">-</span> <span style="color:#ae81ff">24</span><span style="color:#f92672">*</span>x(<span style="color:#ae81ff">4</span>)];
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% ---------------------------------------- 结果 ----------------------------------------</span>
</span></span><span style="display:flex;"><span>Local minimum found that satisfies the constraints.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Optimization completed because the objective <span style="color:#66d9ef">function</span> is non<span style="color:#f92672">-</span>decreasing in 
</span></span><span style="display:flex;"><span>feasible directions, to within the value of the optimality tolerance,
</span></span><span style="display:flex;"><span>and constraints are satisfied to within the value of the constraint tolerance.
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;</span>stopping criteria details<span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>x =
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.9638</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">0.9276</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-</span><span style="color:#ae81ff">0.2172</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">0.0695</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.1448</span>
</span></span><span style="display:flex;"><span>f0 =
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.1448</span>
</span></span><span style="display:flex;"><span>flag =
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">1</span>
</span></span></code></pre></div><h5 id="fmincon-函数的全局优化">fmincon 函数的全局优化</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#75715e">%% fmincon 函数的全局优化</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 由于 fmincon 函数求解，依靠初始值， </span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 该全局优化函数，主要采用循环结构， 产生多个随机数赋值给fmincon函数作为初始值，</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 依次调用fmincon函数求解原始问题， 并比较每次得出的目标函数值，并记录最小的目标函数值</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 这样，就可能得出原始问题的全局最优解。</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> [x,f0,flag] = <span style="color:#a6e22e">fmincon_global</span>(f,a,b,n,N,varargin)
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 参数解释：</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% f 可以是结构体变量，也可以是目标函数的函数句柄</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% a 与b为决策变量所在的区间， 即 自变量x的上下限，可以是向量</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% n 为决策变量的个数， 即自变量的个数</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% N 产生多少个初值， 一般5~10个就好了</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% varargin 一些其他参数，应该包含描述约束的参数，与fmincon()函数完全一致</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 即，fmincon 函数调用中除了 f与x0之外所有的后续变元</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 返回值： </span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% x 很有可能是问题的全局最优解</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% f0 为最优目标函数</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 函数调用格式：</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% x = fmincon(problem,a,b,n,N)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% x = fmincon(f,a,b,n,N,A,b,Aeq,beq)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% x = fmincon(f,a,b,n,N,A,b,Aeq,beq,lb,ub)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% x = fmincon(f,a,b,n,N,A,b,Aeq,beq,lb,ub,nonlcon)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% x = fmincon(f,a,b,n,N,A,b,Aeq,beq,lb,ub,nonlcon,options)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% x = fmincon(problem)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% [x,fval] = fmincon(___)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% [x,fval,exitflag,output] = fmincon(___)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% [x,fval,exitflag,output,lambda,grad,hessian] = fmincon(___)</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%%</span>
</span></span><span style="display:flex;"><span>x0 = rand(n,<span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>k0 = <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 处理结构体</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> strcmp(class(f),<span style="color:#e6db74">&#39;struct&#39;</span>)
</span></span><span style="display:flex;"><span>    k0=<span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> k0<span style="color:#f92672">==</span><span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>    f.x0 = x0;
</span></span><span style="display:flex;"><span>    [x,f0,flag] = fmincon(f); <span style="color:#75715e">%结构体描述的问题直接求解</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>    [x,f0,flag] = fmincon(f,x0,varargin{:}); <span style="color:#75715e">%非结构体描述的问题直接求解</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> flag <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>    f0 = <span style="color:#ae81ff">1e10</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> i = <span style="color:#ae81ff">1</span>:N
</span></span><span style="display:flex;"><span>    x0 = a(:) <span style="color:#f92672">+</span> (b(:) <span style="color:#f92672">-</span> a(:))<span style="color:#f92672">.*</span> rand(n,<span style="color:#ae81ff">1</span>);<span style="color:#75715e">% 用循环结构尝试不同的随机搜索初值</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> k0 <span style="color:#f92672">==</span><span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>        f.x0 = x0;
</span></span><span style="display:flex;"><span>        [x1,f1,flag] = fmincon(f); <span style="color:#75715e">%结构体描述的问题直接求解</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>        [x1,f1,flag] = fmincon(f,x0,varargin{:}); <span style="color:#75715e">%非结构体描述的问题直接求解</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> flag <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">&amp;</span> f1 <span style="color:#f92672">&lt;</span> f0
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">% 如果找到更改的解，则保存</span>
</span></span><span style="display:flex;"><span>        x = x1;
</span></span><span style="display:flex;"><span>        f0 = f1;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>求解上述问题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>clc,clear all;
</span></span><span style="display:flex;"><span>A = [<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.25</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.25</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.5</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">1</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">0.5</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">0</span>,  <span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">0</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>];
</span></span><span style="display:flex;"><span>b = [<span style="color:#f92672">-</span><span style="color:#ae81ff">2.25</span>; <span style="color:#ae81ff">2.25</span>; <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>; <span style="color:#ae81ff">1.5</span>; <span style="color:#f92672">-</span><span style="color:#ae81ff">1.5</span>; <span style="color:#ae81ff">1.5</span>];
</span></span><span style="display:flex;"><span>P.objective = @(x)x(<span style="color:#ae81ff">5</span>);
</span></span><span style="display:flex;"><span>P.Aineq = A;
</span></span><span style="display:flex;"><span>P.Bineq = b;
</span></span><span style="display:flex;"><span><span style="color:#75715e">%P.lb = [];</span>
</span></span><span style="display:flex;"><span>P.nonlcon = @mynocon;
</span></span><span style="display:flex;"><span>P.solver = <span style="color:#e6db74">&#39;fmincon&#39;</span>;
</span></span><span style="display:flex;"><span>P.options = optimset;
</span></span><span style="display:flex;"><span>P.x0 = rand(<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>tic <span style="color:#75715e">% 计算耗时</span>
</span></span><span style="display:flex;"><span>[x,f0,flag] = fmincon_global(P,<span style="color:#f92672">-</span><span style="color:#ae81ff">10</span>,<span style="color:#ae81ff">10</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">10</span>) <span style="color:#75715e">%给出初值并求解</span>
</span></span><span style="display:flex;"><span>toc
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% ---------------------------------------- 结果 ----------------------------------------</span>
</span></span><span style="display:flex;"><span>x =
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">2.4544</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.9088</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">2.7263</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.3510</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">0.8175</span>
</span></span><span style="display:flex;"><span>f0 =
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">0.8175</span>
</span></span><span style="display:flex;"><span>flag =
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>历时 <span style="color:#ae81ff">1.545311</span> 秒。
</span></span></code></pre></div><h3 id="模型-常用非线性规划模型我">模型: 常用非线性规划模型(我)</h3>
<p>$$
\begin{aligned}
Min \quad &amp; \sum_{i=1}^{n} \sum_{j=1}^{n}\left(\omega_{i}-a_{i j} \omega_{j}\right)^{2}\<br>
s.t \quad &amp; \sum_{i=1}^{n} \omega_{i}=1,\
&amp; 1\geq \omega_{i} \geq 0, \quad i=1,2, \ldots, n
\end{aligned}
$$</p>
<p><strong>Matlab模板</strong></p>
<p>把上述文件存储为<code>fmin.m</code>，以后只需要更改目标函数<code>myobjfun(x,AA)</code>即可,其中x代表未知数, AA 代表目标函数的系数，</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> x = <span style="color:#a6e22e">fmin</span>(AA) <span style="color:#75715e">% 调用函数接口</span>
</span></span><span style="display:flex;"><span>n = size(AA,<span style="color:#ae81ff">1</span>) ; <span style="color:#75715e">% 未知数x的长度 </span>
</span></span><span style="display:flex;"><span>x0 = repelem(<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span>n,n);    <span style="color:#75715e">% 初始迭代位置</span>
</span></span><span style="display:flex;"><span>Aeq = repelem(<span style="color:#ae81ff">1</span>,n);     <span style="color:#75715e">% 线性等式约束的系数（左边的系数）</span>
</span></span><span style="display:flex;"><span>beq = <span style="color:#ae81ff">1</span>;                <span style="color:#75715e">% 线性等式约束的值 （右边的值）</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%A = [];                % 线性不等式约束的系数</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%b = [];                % 线性等式约束的值</span>
</span></span><span style="display:flex;"><span>ub = repelem(<span style="color:#ae81ff">1</span>,n);      <span style="color:#75715e">% 变量的上限（取等号）</span>
</span></span><span style="display:flex;"><span>lb = repelem(<span style="color:#ae81ff">0</span>,n);      <span style="color:#75715e">% 变量的下限（取等号）</span>
</span></span><span style="display:flex;"><span>[x,fval] = fmincon(@(x)myobjfun(x,AA),x0,[],[],Aeq,beq,lb,ub);
</span></span><span style="display:flex;"><span><span style="color:#75715e">%[x,fval] = fmincon(@myobjfun,x0,[],[],Aeq,beq,lb,ub,[],[],options,A);</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 目标函数</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> s = <span style="color:#a6e22e">myobjfun</span>(x,AA)
</span></span><span style="display:flex;"><span>n = size(AA,<span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>s =<span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> i = <span style="color:#ae81ff">1</span>:n
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> j = <span style="color:#ae81ff">1</span>:n
</span></span><span style="display:flex;"><span>        s= s<span style="color:#f92672">+</span> (x(i)<span style="color:#f92672">-</span> AA(i,j)<span style="color:#f92672">*</span>x(j))^<span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>参考:</p>
<ul>
<li>
<p>官网函数解释https://www.mathworks.com/help/optim/ug/fmincon.html</p>
</li>
<li>
<p><a href="https://wenku.baidu.com/view/6bcb651d0b4e767f5acfce97.html?sxts=1561344526517">https://wenku.baidu.com/view/6bcb651d0b4e767f5acfce97.html?sxts=1561344526517</a></p>
</li>
<li>
<p><a href="https://blog.csdn.net/qq_38784454/article/details/80329021">https://blog.csdn.net/qq_38784454/article/details/80329021</a></p>
</li>
<li>
<p>《MATLAB数学建模》李昕——清华大学出版</p>
</li>
<li>
<p>《MATLAB R2015b最优化计算》&ndash;李娅</p>
</li>
</ul>

        </section>
    </div>
    <br>
    
    




<span id="/md/2021-05-16-matlab%E4%BC%98%E5%8C%96%E5%B7%A5%E5%85%B702%E9%9D%9E%E7%BA%BF%E6%80%A7%E8%A7%84%E5%88%92%E4%B9%8Bfmincon/" class="leancloud_visitors" data-flag-title="matlab优化工具02非线性规划之fmincon">
  <span class="post-meta-item-text">文章总阅读量 </span>
  <span class="leancloud-visitors-count"><i class="leancloud-visitors-count"></i></span>次;
  <p></p>
</span>



    

    
    
    <button id="edit-button" class="icon-button" type="button" title="Fork and edit" aria-label="Fork and edit" aria-haspopup="true" aria-expanded="false" aria-controls="edit">
        <i class="fa fa-edit">编辑本文</i>
    </button>
    
    
    

    <br>
    <hr>
    <li style="float:left;list-style:none">
        
        <a class="previous" href="/md/2021-05-16-matlab%E4%BC%98%E5%8C%96%E5%B7%A5%E5%85%B703%E5%85%A8%E5%B1%80%E4%BC%98%E5%8C%96%E4%B9%8Bga/"> 上一篇: matlab优化工具03全局优化之ga</a>
        
    </li>
    <li style="float:right;list-style:none">
        
        <a class="next" href="/md/2021-05-16-matlab%E4%BC%98%E5%8C%96%E5%B7%A5%E5%85%B701%E7%BA%BF%E6%80%A7%E8%A7%84%E5%88%92%E4%B9%8Blinprog/"> 下一篇: matlab优化工具01线性规划之linprog</a>
        
    </li>
     
    
    <script src="/js/copyCode.js"></script>
    <script src="/js/tooltips.js"></script>
    
   
    <script>
    [].slice.call(document.querySelectorAll('table')).forEach(function(el) {
        var wrapper = document.createElement('div');
        wrapper.className = 'table-area';
        el.parentNode.insertBefore(wrapper, el);
        el.parentNode.removeChild(el);
        wrapper.appendChild(el);
        $("table").wrap("<div class='table-area'></div>");
    })
    </script>

    
<br>
<hr>


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-111691389-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', 'UA-111691389-1');
</script>




      
      
      

       
      
      
      <script>
              document.getElementById("edit-button").addEventListener("click", function () {
                  var editWindow = window.open("https:\/\/github.com\/zoushucai\/blogmmm/edit/master/content/md\/2021-05-16-matlab优化工具02非线性规划之fmincon.md");
              });</script>
      
          




<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  } 
</script>



    </style>
    <script type="text/javascript">
    function showdiv(){
        document.getElementById("divtocTableOfContents").style.display="block";
        document.getElementById("strHref").innerHTML="目录收起-";
        document.getElementById('divTableOfContents').style.width="22%";
        document.getElementById('divTableOfContents').style.height="55%";
        document.getElementById('divTableOfContents').style.top="25%";
        document.getElementById('divTableOfContents').style.bottom="5%";
        document.getElementById("strHref").href="javascript:hidediv()";
    }
    function hidediv(){
        document.getElementById("divtocTableOfContents").style.display="none";
        document.getElementById("strHref").innerHTML="目录展开+";
        document.getElementById("strHref").href="javascript:showdiv()";
        document.getElementById('divTableOfContents').style.width="10%";
        document.getElementById('divTableOfContents').style.height="5%";
    }
    </script>
</body>

</html>
</div> 







    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-LJ2FmexL77rmGm6SIpxq7y+XA6bkLzGZEgCywzKOZG/ws4va9fUVu2neMjvc3zdv" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            renderMathInElement(document.body, {
            delimiters: [
                            {left: "$$", right: "$$", display: true},
                            {left: "$", right: "$", display: false},
                            {left: "\\(", right: "\\)", display: false},
                            {left: "\\[", right: "\\]", display: true}
                        ]
            });
        });
    </script>













<br>
<div class="inner">
              
            
          
          
  
          
  
  <div id="vcomments"></div>
  
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'HfHPKPkLa0cBEDPcdBAHuqMv-gzGzoHsz',
        appKey: 'r5RJAasN8e0mB9sq6y9pEcX0',
        lang:'zh-CN',
        notify:  false , 
        verify:  false  ,
        avatar:'identicon', 
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

</div>

<br>
<br>
<footer>
    <p style="float:right;margin-right: 5%;margin-top: 0%;">
        &copy; 2022 <a href="https://github.com/zoushucai">zsc</a>
      </p>
</footer>
<br>
<br>


